'use strict'

const { Controller } = require('egg')

class HomeController extends Controller {
  // 获取请求信息列表
  async index() {
    const { ctx, service } = this
    await ctx.isLogin()
    ctx.body = await service.log.list()
  }

  // 删除请求信息
  async destroy() {
    const { ctx, service } = this
    await ctx.isLogin()
    await ctx.validateId()
    const log = await ctx.model.Log.findById(ctx.params.id)
    if (!log) {
      ctx.throw(404, 'not found', '对象不存在')
    }
    if (log.user.toString() !== ctx.user._id.toString() && !ctx.user.isAdmin) {
      ctx.throw(403, 'not permission', '无权删除')
    }
    ctx.body = await log.remove()
  }
}

module.exports = HomeController
